var li_wall_bg_color = 'even';
var COMMON_IMAGES_PATH = base_url+'';
var i1 = 2;
var next_ctr=0;
var base_url;
var avatar_ctr = 1;
var wall_owner = 0;

$(document).ready(
    function(){
        base_url = $(".base-url").val();
        
        func.submit_message();
        
        func.show_all_message();
        
        func.edit_profile({index:'.view-complete-profile'});
    }
);

var func = {
    init_js_achor:function(param){
        switch(param.id){
            case 'd_profile':
                func.edit_profile({index:'.view-complete-profile'});
                break;
            case 'd_friends':
                func.friends_wall();
                break;
            default:
                break;
        } 
    },
    friends_wall:function(){
        $(".d-friend-avatar-wrapper img").click(function(){
            //alert($(this).attr('id'));
            wall_owner = $(this).attr('id');
            //alert(base_url+'profile/avatar/376/281/'+wall_owner+'/');
                
            
            var url = base_url+'profile/get_friends_wall';
            var pass ={'wall_owner':wall_owner};
            $.post(url,pass,function(data){
                $(".d-profilepic img").attr('src',data.src).load(function(){
                    $(this).fadeIn(3000);
                });
                $(".wall-messages").html(data.message_list);
            },'json');
        }).css('cursor','pointer');
    },
    submit_message:function(){
        $(".submit-wall-message").click(function(){
            var msg =  $(".d-wall-shout-box textarea").val();
            var param = {message:msg,'wall_owner':wall_owner};
            
            if(msg){
                $(this).attr('disabled','disabled');
                $(".preloader").show();
                $.post(base_url+'profile/submit_message', param, function(data){
                    $(".d-wall-shout-box textarea").val('');
                    $(".wall-messages").html(data.content);
                    $(".wall-messages li:first").hide();
                    $(".wall-messages li:first").fadeIn();
                    $(".preloader").hide();
                    $(".submit-wall-message").removeAttr('disabled');
                    next_ctr = 0;
                },'json');
            }
        });
    },
    show_all_message:function(){
        $(".d-wall-older-posts a").click(function(){
            var url = base_url+'profile/get_next_wall_message';
            var param={next:next_ctr,'wall_owner':wall_owner};
            
            $(".preloader-messages").show();
            $.post(url, param, function(data){
                next_ctr=data.next;
                
                $(".preloader-messages").hide();
                $(".wall-messages").html(data.messages);
                
            }, 'json');
            return false;
        });
    },
    edit_profile:function(param){
        $(param.index).fancybox({
            'transition': 'elastic',
            'titleShow' : false,
            'onComplete':this.modify_profile
        });
    },
    modify_profile:function(){
        
        var index = '.my-ajax-form';
        $(index).validate({
            rules:{
                password: {
                    maxlength:128
                },
                confirm_password: {
                    equalTo: "#password"
                }
            }
        });
        obj.ajax_submit({
            index:index,
            data_type:'json',
            before_submit:function(){
                obj.enabled({active:false,index:'input[type="submit"]'})
                $(".preloader").show();
            },
            ajax_success:function(data){
                obj.enabled({active:true,index:'input[type="submit"]'})
                $(".preloader").hide();
                if(obj.callBack({success:data.success,message:data.message})){
                    $.fancybox.close();
                    $('a[id="d_profile"]').click();
                    $(".d-profilepic img").attr('src',base_url+'profile/avatar/376/281/user/'+(avatar_ctr++)).load(function(){$(this).fadeIn(3000)});
                }
            }
        });
        
        $("input[name='birthdate']").datepicker({
            showOn : 'button',
            buttonImage : base_url+'common/assets/images/calendar.gif',
            buttonImageOnly: true,
            appendText: '(mm/dd/yyyy)'
        });
    }
};

var obj = {
    callBack:function(param){
        var ret = false;
        var msg = param.message;
        switch(param.success){
            case 'yes':
                ret = true;
                break;
            default:
                break;
        }
        if(!ret)alert(msg);
        return ret;
    },
    enabled:function(param){
        (param.active)?$(param.index).removeAttr('disabled'):$(param.index).attr('disabled', 'disabled');
    },
    ajax_submit:function(param){
        $(param.index).ajaxForm({
            dataType:param.data_type,
            beforeSubmit:param.before_submit,
            success:param.ajax_success
        });
    },
    a_submit:function(param){
        $(param.index).ajaxForm({
            'dataType':param.dataType,
            'beforeSubmit':param.beforeSubmit,
            'success':param.success
        });
    }
};